<?
//Positions 0.1.2
class positions{
    private $id;
    private $name;
    private $position;
    private $symbiont;
    private $access_level;
    private $exists=false;
    function __construct($id="", $name="", $position="", $symbiont="", $access_level=""){
        $this->id=$id;
        $this->name=$name;
        $this->position=$position;
        $this->symbiont=$symbiont;
        $this->access_level=$access_level;
        $this->read();
    }
    //Read data from data base
    function read(){
        global $sql;
        if($this->id!=""&&$this->name==""){
            if($this->position==""){
                $what=array('position', 'symbiont', 'access_level');
                $where=array('id'=>$this->id);
                $order="position";
                $limit="";
            }
            else{
                $what=array('symbiont', 'access_level');
                $where=array('id'=>$this->id, 'position'=>$this->position);
                $order="";
                $limit=1;
            }
        }
        elseif($this->name!=""){
            if($this->position==""){
                $what=array('id', 'position', 'symbiont');
                $where=array('name'=>$this->name);
                $order="position";
                $limit="";
            }
            else{
                $what=array('id', 'symbiont');
                $where=array('name'=>$this->name, 'positions'=>$this->position);
                $order="";
                $limit=1;
            }
        }
        if($this->access_level){
            $where['access_level']=$this->access_level;
        }
        else{
            $what[]="access_level";
        }
        $r=$sql->select_each("positions", $what, $where, "", $order, $limit);
        if(is_array($r)){
            $this->exists=true;
            foreach($r as $k=>$v){
                if(isset($v[0])&&is_array($v[0])){
                    if(isset($v[0]['id'])) $this->id=$v[0]['id'];
                    foreach($v as $key->$val){
                        if(!is_array($this->symbiont)) $this->symbiont=array();
                        $this->symbiont[]=$val['symbiont'];
                        if(!is_array($this->access_level)) $this->access_level=array();
                        $this->access_level[]=$val['access_level'];
                    }
                }
                else{
                    if(!is_array($this->symbiont)) $this->symbiont=array();
                    $this->symbiont[]=$v['symbiont'];
                    if(!is_array($this->access_level)) $this->access_level=array();
                    $this->access_level[]=$v['access_level'];
                    if(isset($v['id'])) $this->id=$v['id'];
                }
            }
        }
        else{
            $this->exists=false;
        }
    }
    //Get the data
    function get($what){
        return $this->$what;
    }
    //Add postion
    function add(){
        global $sql;
        if($this->id!=""&&$this->position!=""){
            $sql->query("UPDATE `".$sql->tables->positions."` SET(`position`=`position`+1) WHERE `id`=".$this->id." AND `position`>".$this->position."");
            if($this->name=="") $this->name=$sql->select_each($sql->tables->positions, 'name', array('id'=>$this->id), "", "", 1);
            if($this->name=="") return false;
            if($this->access_level=="") $this->access_level=0;
            if($this->symbiont=="") return false;
            $r=$sql->insert($sql->tables->positions, array('id'=>$this->id, 'position'=>$this->position, 'name'=>$this->name, 'symbiont'=>$this->symbiont, 'access_level'=>$this->access_level));
            return $r;
        }
        elseif($this->id!=""){
            $max=$sql->query_each("SELECT max(`position`) FROM `".$sql->tables->positions."` WHERE `id` = '".$id."' LIMIT 1");
            $this->position=$max+1;
            if($this->name=="") $this->name=$sql->select_each($sql->tables->positions, 'name', array('id'=>$this->id), "", "", 1);
            if($this->name=="") return false;
            if($this->access_level="") $this->access_level=0;
            if($this->symbiont=="") return false;
            $r=$sql->insert($sql->tables->positions, array('id'=>$this->id, 'position'=>$this->position, 'name'=>$this->name, 'symbiont'=>$this->symbiont, 'access_level'=>$this->access_level));
            return $r;
        }
        elseif($this->name!=""&&$this->position!=""){
            $this->id=$sql->select($sql->tables->positions, 'id', array('name'=>$this->name), "", "", 1);
            if($this->id){
                $sql->query("UPDATE `".$sql->tables->positions."` SET(`position`=`position`+1) WHERE `id`=".$this->id." AND `position`>".$this->position."");
                if($this->access_level=="") $this->access_level=0;
                if($this->symbiont=="") return false;
                $r=$sql->insert($sql->tables->positions, array('id'=>$this->id, 'position'=>$this->position, 'name'=>$this->name, 'symbiont'=>$this->symbiont, 'access_level'=>$this->access_level));
                return $r;
            }
            else{
                if($this->access_level=="") $this->access_level=0;
                if($this->symbiont=="") return false;
                $r=$sql->insert($sql->tables->positions, array('name'=>$this->name, 'position'=>$this->position, 'symbion'=>$this->symbiont, 'access_level'=>$this->access_level));
                return $r;
            }
            
        }
        elseif($this->name!=""){
            $this->id=$sql->select($sql->tables->positions, 'id', array('name'=>$this->name), "", "", 1);
            if($this->id){
                $max=$sql->query_each("SELECT max(`position`) FROM `".$sql->tables->positions."` WHERE `id` = '".$id."' LIMIT 1");
                $this->position=$max+1;
                if($this->access_level="") $this->access_level=0;
                if($this->symbiont=="") return false;
                $r=$sql->insert($sql->tables->positions, array('id'=>$this->id, 'position'=>$this->position, 'name'=>$this->name, 'symbiont'=>$this->symbiont, 'access_level'=>$this->access_level));
                return $r;
            }
            else{
                $this->position=1;
                if($this->access_level="") $this->access_level=0;
                if($this->symbiont=="") return false;
                $r=$sql->insert($sql->tables->positions, array('position'=>$this->position, 'name'=>$this->name, 'symbiont'=>$this->symbiont, 'access_level'=>$this->access_level));
                return $r;
            }
        }
        return false;
    }
    //Delete position
    function del(){
        global $sql;
        if($this->id!=""&&$this->position!=""&&!is_array($this->position)){
            $r=$sql->delete($sql->tables->positions, array('id'=>$this->id, 'position'=>$this->position), 1);
            $r=$r&&$sql->query("UPDATE `".$sql->tables->positions."` SET(`position`=(`position`-1)) WHERE `id`=".$this->id." AND `position`>".$this->position."");
            return $r;
        }
        elseif($this->id!=""){
            $r=$sql->delete($sql->tables->positions, array('id'=>$this->id), 1);
            return $r;
        }
        elseif($this->name!=""&&$this->position!=""&&!is_array($this->position)){
            $r=$sql->delete($sql->tables->positions, array('name'=>$this->name, 'position'=>$this->position), 1);
            $r=$r&&$sql->query("UPDATE `".$sql->tables->positions."` SET(`position`=(`position`-1)) WHERE `id`=".$this->id." AND `position`>".$this->position."");
            return $r;
        }
        elseif($this->name!=""){
            $r=$sql->delete($sql->tables->positions, array('name'=>$this->name));
            return $r;
        }
        return false;
    }
    //Edit position
    function edit($id, $name, $position, $symbiont, $access_level=0){
        if($symbiont!="") $this->symbiont=$symbiont;
        if($access_level!="")  $this->access_level=$access_level;
        if($this->id&&$this->position){
            $values=array();
            if($id=="") $id=$this->id;
            elseif($id!=$this->id) $values['id']=$id;
            if($name!="") $this->name=$name;
            if($this->name!="") $values['name']=$this->name;
            if($this->symbiont!="") $values['symbiont']=$this->symbiont;
            if($this->access_level!="") $values['access_level']=$this->access_level;
            $where=array('id'=>$this->id, 'postion'=>$this->position);
            $r=$sql->update($sql->tables->positions, $values, $whеre, 1);
            if($position!=""){
                $r=$r&&$sql->update($sql->tables->positions, array('position'=>0), array('id'=>$this->id, 'position'=>$this->position));
                $r=$r&&$sql->update($sql->tables->positions, array('position'=>$this->position), array('id'=>$this->id, 'position'=>$position));
                $r=$r&&$sql->update($sql->tables->positions, array('position'=>$position), array('id'=>$this->id, 'position'=>$this->position));
                $this->position=$position;
            }
            $this->id=$id;
            return $r;
        }
        elseif($this->id){
            if($name!="") $this->name=$name;
            $values=array();
            if($this->name!="") $values['name']=$this->name;
            if($this->access_level!="") $values['access_level']=$this->access_level;
            $where=array('id'=>$this->id);
            if(count($values)==0) return false;
            $r=$sql->update($sql->tables->positions, $values, $where);
            return $r;
        }
        elseif($this->name&&$this->position){
            $values=array();
            if($name=="") $name=$this->name;
            elseif($this->name!=$name) $values['name']=$name;
            if($this->symbiont!="") $values['symbiont']=$this->symbiont;
            $where=array('name'=>$this->name, 'position'=>$this->position);
            $r=$sql->update($sql->tables->positions, $values, $where);
            if($position!=""){
                $r=$r&&$sql->update($sql->tables->positions, array('position'=>0), array('name'=>$this->name, 'position'=>$this->position));
                $r=$r&&$sql->update($sql->tables->positions, array('position'=>$this->position), array('name'=>$this->name, 'position'=>$position));
                $r=$r&&$sql->update($sql->tables->positions, array('position'=>$position), array('name'=>$this->name, 'position'=>$this->position));
                $this->position=$position;
            }
            $this->name=$name;
            return $r;
        }
        elseif($this->name){
            $values=array();
            if($this->access_level!="") $values['access_level']=$this->access_level;
            $where=array('name'=>$this->name);
            if(count($values)==0) return false;
            $r=$sql->update($sql->tables->positions, $values, $where);
            return $r;
        }
        return false;
    }
    function prints(){
        global $kernel, $sym, $user;
        if(is_array($this->symbiont)){
            $c=count($this->symbiont);
            for($i=0; $i<$c; $i++){
                if($this->access_level[$i]>$user->access_level) continue;
                if($this->symbiont[$i]!=""){
                    $s=explode('|', $this->symbiont[$i]);
                    list($symb, $inc)=design::symbiont($s[0], isset($s[1])?$s[1]:'');
					eval($inc);
                    $kernel->inc();
                    eval($symb.";");
                }
            }
        }
        else{
            if($this->symbiont!=""){
                list($symb, $inc)=design::symbiont($this->symbiont);
				eval($inc);
				eval($symb.";");
            }
        }
    }
    function __get($key){
        return $this->$key;
    }
    function __set($key, $val){
        $this->$key=$val;
    }
}
?>